#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

void writer(int wfd)
{
    const char *str="This is a child process";
    char buffer[128];
    while(1)
    {
        snprintf(buffer,sizeof(buffer),"The string is %s",str);
        write(wfd,buffer,sizeof(buffer));
        sleep(1);
    }
}

void reader(int rfd)
{
    char buffer[1024];
    while(1)
    {
        read(rfd,buffer,sizeof(buffer)-1);
        printf("father get a message: %s", buffer);
    }

}



int main()
{
    int pipefd[2];
    int n=pipe(pipefd);
    if(n<0)
    {
        return 1;
    }
    printf("read:%d,write:%d",pipefd[0],pipefd[1]);

    pid_t id=fork();
    if(id==0)
    {
        //child  w
        close(pipefd[0]);
        writer(pipefd[1])
        exit(0);
    }

    //father r
    close(pipefd[1]);
    reader(pipefd[0]);
    wait(NULL);

    return 0;
}